#!/usr/bin/env python3
"""
ComfyUI GUI 应用示例
"""

import tkinter as tk
from tkinter import ttk, messagebox
import threading
import sys
import os

class ComfyUIGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("ComfyUI 管理工具")
        self.root.geometry("600x400")
        
        # 设置样式
        style = ttk.Style()
        style.theme_use('clam')
        
        self.setup_ui()
    
    def setup_ui(self):
        """设置用户界面"""
        # 主框架
        main_frame = ttk.Frame(self.root, padding="10")
        main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
        
        # 标题
        title_label = ttk.Label(main_frame, text="ComfyUI 依赖管理工具", 
                               font=('Arial', 16, 'bold'))
        title_label.grid(row=0, column=0, columnspan=2, pady=(0, 20))
        
        # 功能按钮
        ttk.Button(main_frame, text="环境配置", 
                  command=self.show_env_config).grid(row=1, column=0, pady=5, padx=5, sticky='ew')
        
        ttk.Button(main_frame, text="依赖管理", 
                  command=self.show_depend_manager).grid(row=1, column=1, pady=5, padx=5, sticky='ew')
        
        ttk.Button(main_frame, text="启动 ComfyUI", 
                  command=self.start_comfyui).grid(row=2, column=0, pady=5, padx=5, sticky='ew')
        
        ttk.Button(main_frame, text="系统信息", 
                  command=self.show_system_info).grid(row=2, column=1, pady=5, padx=5, sticky='ew')
        
        # 日志区域
        log_frame = ttk.LabelFrame(main_frame, text="操作日志", padding="5")
        log_frame.grid(row=3, column=0, columnspan=2, pady=(20, 0), sticky='ew')
        
        self.log_text = tk.Text(log_frame, height=10, width=70)
        scrollbar = ttk.Scrollbar(log_frame, orient="vertical", command=self.log_text.yview)
        self.log_text.configure(yscrollcommand=scrollbar.set)
        
        self.log_text.grid(row=0, column=0, sticky='ew')
        scrollbar.grid(row=0, column=1, sticky='ns')
        
        # 状态栏
        self.status_var = tk.StringVar()
        self.status_var.set("就绪")
        status_bar = ttk.Label(main_frame, textvariable=self.status_var, relief='sunken')
        status_bar.grid(row=4, column=0, columnspan=2, pady=(10, 0), sticky='ew')
    
    def log(self, message):
        """添加日志"""
        self.log_text.insert(tk.END, f"{message}\n")
        self.log_text.see(tk.END)
        self.root.update_idletasks()
    
    def show_env_config(self):
        """显示环境配置"""
        self.log("正在检查环境配置...")
        try:
            from env_setup import setup_environment, print_environment_info
            config = setup_environment()
            self.log("环境配置检查完成")
            messagebox.showinfo("环境配置", "环境配置检查完成，请查看日志")
        except Exception as e:
            self.log(f"环境配置检查失败: {e}")
            messagebox.showerror("错误", f"环境配置检查失败: {e}")
    
    def show_depend_manager(self):
        """显示依赖管理"""
        self.log("正在启动依赖管理器...")
        try:
            from depend_manager import main as depend_main
            # 在新线程中运行，避免阻塞GUI
            thread = threading.Thread(target=depend_main)
            thread.daemon = True
            thread.start()
            self.log("依赖管理器已启动")
        except Exception as e:
            self.log(f"依赖管理器启动失败: {e}")
            messagebox.showerror("错误", f"依赖管理器启动失败: {e}")
    
    def start_comfyui(self):
        """启动 ComfyUI"""
        self.log("正在启动 ComfyUI...")
        try:
            # 这里可以添加启动 ComfyUI 的逻辑
            self.log("ComfyUI 启动命令已执行")
            messagebox.showinfo("启动", "ComfyUI 启动命令已执行")
        except Exception as e:
            self.log(f"ComfyUI 启动失败: {e}")
            messagebox.showerror("错误", f"ComfyUI 启动失败: {e}")
    
    def show_system_info(self):
        """显示系统信息"""
        self.log("正在获取系统信息...")
        try:
            import psutil
            info = f"""
系统信息:
- CPU 核心数: {psutil.cpu_count()}
- 内存总量: {psutil.virtual_memory().total // (1024**3)} GB
- Python 版本: {sys.version}
- 当前目录: {os.getcwd()}
            """
            self.log(info)
            messagebox.showinfo("系统信息", info)
        except Exception as e:
            self.log(f"获取系统信息失败: {e}")
            messagebox.showerror("错误", f"获取系统信息失败: {e}")

def main():
    """主函数"""
    root = tk.Tk()
    app = ComfyUIGUI(root)
    
    # 配置网格权重
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    
    # 启动GUI
    root.mainloop()

if __name__ == "__main__":
    main() 